<?php
/**
 * Load styles and scripts
 * 
 */
include 'theme_walker.php';
include 'admin/larch_admin.php';
function larch_scripts(){
	wp_enqueue_style('bootstrap', get_template_directory_uri().'/assets/css/bootstrap.css');
	wp_enqueue_style('style',  get_stylesheet_uri());
	wp_enqueue_script('jquery-1.10.1.min',  get_template_directory_uri().'/assets/js/jquery-1.10.1.min.js');
	wp_enqueue_script('bootstrap',  get_template_directory_uri().'/assets/js/bootstrap.js');
}
add_action( 'wp_enqueue_scripts', 'larch_scripts' );

/**
 * Setting default content width
 * 
 */
if(!isset($content_width)){
	$content_width = 474;
}

if(!function_exists( 'larch_setup')){
	function larch_setup(){
		//load text domain
		load_theme_textdomain('larch',  get_template_directory().'/lang');
		add_theme_support('post-thumbnails');
		set_post_thumbnail_size(672,372,true);
		add_image_size('larch-full-width', 1038, 576, true);
		//register navigations
		register_nav_menus(array(
			'primary' => __('Main menu','larch'),
			'secondary' => __('Sidebar menu','larch'),
			'ternary' => ('Other navigation on a single page'),
		));
		add_theme_support('html5',array(
			'search-form','comment-form','comment-list'
		));
		add_theme_support('post-formats',array(
			'image','video','link','gallery'
		));
		add_theme_support('custom-background',array('default-color'=>'f5f5f5'));
		
	}
}
add_action('after_setup_theme','larch_setup');
if(!function_exists( 'register_navs')){
	function register_navs(){
		register_sidebar(array(
			'name'=>__('Main Navigation','larch'),
			'id'=>('pseudo-menu'),
			'description'=>__('navigation','larch'),
		));
	}
}
add_action('widgets_init','register_navs');

/**
 * Theme options
 * 
 */
add_action('admin_menu','larch_admin_menu');
if(!function_exists( 'larch_admin_menu')){
	function larch_admin_menu(){
		add_menu_page('Larch Theme Options', 'R Dan Theme Options', 'administrator', 'larch_admin_options_page', 'create_larch_admin');
	}
}
/**
 * Create Admin 
 * 
 */
function create_larch_admin(){
	Larch_admin::larch_admin_page_layout();
}
function init_larch_admin_options(){
	//check if there's an option existing
	if(false== get_option('larch_admin_general_options')){ //custom option name
		add_option('larch_admin_general_options');
	}

	//create section then put it inside the newly created option above
	add_settings_section('larch_header_section','Header Settings','header_section','larch_admin_header_options');//this would also add the section to the newly created option
	add_settings_field('larch_show_social_icons', 'Social Icons', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_show_social_icons',
		'std' => '',
		'type' => 'checkbox',
		'desc' => 'show icons',
	));
	add_settings_field('larch_company_logo', 'Company Logo', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_company_logo',
		'std' => '',
		'type' => 'checkbox',
		'desc' => 'show logo?',
	));
	add_settings_field('larch_use_large_header_bg', 'Large Background Image', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_use_large_header_bg',
		'std' => '',
		'type' => 'checkbox',
		'desc' => 'use large background image on the header(screen wide image)',
	));
	add_settings_field('larch_url_fb', 'Facebook Page', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_url_fb',
		'std' => '',
		'type' => 'text',
		'desc' => 'facebook fan page url',
	));
	add_settings_field('larch_url_twitter', 'Twitter', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_url_twitter',
		'std' => '',
		'type' => 'text',
		'desc' => 'twitter url',
	));
	add_settings_field('larch_login', 'Login Button', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_login',
		'std' => '',
		'type' => 'checkbox',
		'desc' => 'let users to login',
	));
	add_settings_field('larch_help', 'Help Button', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_help',
		'std' => '',
		'type' => 'checkbox',
		'desc' => 'give users some help around the site',
	));
	add_settings_field('larch_contact_info', 'Contact Info', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_contact_info',
		'std' => '',
		'type' => 'checkbox',
		'desc' => 'let users contact you',
	));
	add_settings_field('larch_company_address', 'Company Address', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_company_address',
		'std' => '',
		'type' => 'textarea',
		'desc' => '',
	));
	add_settings_field('larch_company_email', 'Company Email', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_company_email',
		'std' => '',
		'type' => 'tel',
		'desc' => '',
	));
	add_settings_field('larch_company_tel', 'Company Contact Number', 'create_field', 'larch_admin_header_options', 'larch_header_section', array(
		'id' => 'larch_company_tel',
		'std' => '',
		'type' => 'tel',
		'desc' => '',
	));
	register_setting('larch_admin_header_options', 'larch_admin_header_options');
}
add_action('admin_init','init_larch_admin_options');
function header_section(){}
function create_field($cb_args){
	extract($cb_args);
	$option_name = 'larch_admin_header_options';
	$larch_settings = get_option($option_name);
	$larch_settings[$id] = esc_attr(stripslashes($larch_settings[$id]));
	switch ( $type) {
		case 'checkbox':
			echo "<input id ='$id' name='".$option_name."[$id]' type='$type' value='1' ".checked(1,$larch_settings[$id],false)."><label for='$id'>$desc</label>";
			break;
		case 'text':
			echo "<input id ='$id' name='".$option_name."[$id]' type='$type' value='$larch_settings[$id]'><label for='$id'>$desc</label>";
			break;
		case 'tel':
			echo "<input id ='$id' name='".$option_name."[$id]' type='$type' value='$larch_settings[$id]'><label for='$id'>$desc</label>";
			break;
		case 'email':
			echo "<input id ='$id' name='".$option_name."[$id]' type='$type' value='$larch_settings[$id]'><label for='$id'>$desc</label>";
			break;
		case 'textarea':
			echo "<textarea name='".$option_name."[$id]' id ='$id'>$larch_settings[$id]</textarea>";
			break;
		default:
			break;
	}
	
}
add_action('admin_enqueue_scripts',Larch_admin::load_admin_scripts());

function ss_menu_terms($atts){
	extract( shortcode_atts( array(
    'tax'           => 'category',
    'display_count' => 'false',
    'columns'       => 5,
 
    'orderby'       => 'name',
    'order'         => 'ASC',
    'hide_empty'    => 'false',
    'exclude'       => array(),
    'exclude_tree'  => array(),
    'include'       => array(),
    'number'        => '',
    //'fields'      => 'all',
    'slug'          => '',
    'parent'        => '',
    'hierarchical'  => 'true',
    'child_of'      => 0,
    'get'           => '',
    'name__like'    => '',
    'pad_counts'    => 'false',
    'offset'        => '',
    'search'        => '',
    'cache_domain'  => 'core'
), $atts ) );
	$display_count = $display_count == 'true' ? true : false;
$hide_empty = $hide_empty == 'true' ? true : false;
$hierarchical = $hierarchical == 'false' ? false : true;
$pad_counts = $pad_counts == 'true' ? true : false;
 
if( strpos( $tax , ',' ) ) $tax = explode( ',' , $tax );
if( !is_array( $exclude ) ) $exclude = explode( ',' , $exclude );
if( !is_array( $exclude_tree ) ) $exclude_tree = explode( ',' , $exclude_tree );
if( !is_array( $include ) ) $include = explode( ',' , $include );
$term_args = compact(
        'orderby' , 'order' , 'hide_empty' , 'exclude' ,
        'exclude_tree' , 'include' , 'number' , 'slug' ,
        'parent' , 'hierarchical' , 'child_of' , 'get' ,
        'name__like' ,  'pad_counts' , 'offset' ,
        'search' , 'cache_domain' );
 
$terms = get_terms( $tax , $term_args );
$n = count( $terms );
$per_column = ceil( $n / $columns );
$html = '<div class="menu-terms menu-terms-col-'.$columns.'">';
 
$html.= '<ul class="menu-terms-column">';
 
foreach( $terms as $i => $term ){
    $html.= '<li class="term-'.$term->term_id.' term-slug-'.$term->slug.' term-taxonomy-'.$term->taxonomy.'">';
    $html.= '<a href="'.get_term_link($term).'">';
    $html.= $term->name;
    if( $display_count ) $html.= ' ('.$term->count. ')';
    $html.= '</a></li>';
 
    if( ($i+1) % $per_column == 0 ){
        $html.= '</ul><ul class="menu-terms-column">';
    }
}
 
$html.= '</ul></div>';
 
return $html;
}
add_shortcode( 'menu-terms' , 'ss_menu_terms' );

function larch_shortcode($attrs){
	$larch_default = array('category_id'=>21);
	extract(shortcode_atts($larch_default, $attrs));
	$html = '<div><ul>';
		$q = query_posts("cat=21");
	if(have_posts()){
		while(have_posts()){the_post();
			$title = the_title();
			$link = the_permalink();
			$html .= "<li><a href='$link'>$title</a></li>";
		}
	}
	wp_reset_query();
	$html .='</ul></div>';
	return $html;
	
}
add_shortcode( 'larch_categories' , 'larch_shortcode' );